﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimalWars2
{
    public abstract class Player : MovableEntity
    {
        public float m_VisionRadius = 30.0f;

        public GameTable m_GameTable;
        public PlayerSide m_Side;
        public PlayerState m_PlayerState;
        private int m_StunTime = 3000;
        private int m_BallThrowDelay = 300;
        private int m_stunnedTimerCount;
        private int m_BallThrowDelayCount;
        public Ball m_Ball;

        protected AnimatedTexture m_PlayerAnimatedTexture;
        protected int m_PlayerAnimationFrameCount = 2;
        protected int m_PlayerAnimationFrameRate = 7;

        public Player(GameTable GameTableRef, Texture2D Tex, Vector2 Pos, Vector2 Vel, PlayerSide side) :
            base(GameTableRef.m_Game, Tex, Pos, Vel)
        {
            m_GameTable = GameTableRef;
            m_Side = side;
            m_PlayerState = PlayerState.Normal;
        }

        bool m_PrevFire = true;

        public abstract bool MoveLeft();
        public abstract bool MoveRight();
        public abstract bool Action();

        public override Vector2 GetMoveDir()
        {
            Vector2 vMoveDir = new Vector2(0.0f, 0.0f);

            m_PlayerAnimatedTexture.Pause();

            if (m_PlayerState != PlayerState.Stunned)
            {
                if (MoveRight() && m_Pos.X < 120.0f)
                {
                    vMoveDir.X = 1.0f;
                    m_PlayerAnimatedTexture.Play();
                }

                if (MoveLeft() && m_Pos.X > -120.0f)
                {
                    vMoveDir.X = -1.0f;
                    m_PlayerAnimatedTexture.Play();
                }
            }

            return vMoveDir;
        }

        public void ChangePlayerState(PlayerState NewPlayerState)
        {
            m_PlayerState = NewPlayerState;

            if (m_PlayerState == PlayerState.Stunned)
            {
                m_stunnedTimerCount = 0;
            }
        }

        public override void Update(GameTime gameTime)
        {
            m_BallThrowDelayCount += gameTime.ElapsedGameTime.Milliseconds;

            if (m_PlayerState == PlayerState.Stunned)
            {
                m_stunnedTimerCount += gameTime.ElapsedGameTime.Milliseconds;

                if (m_stunnedTimerCount > m_StunTime)
                {
                    m_stunnedTimerCount = 0;
                    ChangePlayerState(PlayerState.Normal);
                }
            }
            else if (m_PlayerState == PlayerState.Normal)
            {
                float DeltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

                m_PlayerAnimatedTexture.UpdateFrame(DeltaTime);

                bool spacePressed = Action();

                if (m_Ball != null && (!m_PrevFire) && spacePressed && m_BallThrowDelayCount >= m_BallThrowDelay)
                {
                    m_BallThrowDelayCount = 0;
                    m_Ball.m_BallState = BallState.Moving;
                    m_Ball = null;
                }
                else if (spacePressed)
                {
                    foreach (Entity Current in m_GameTable.m_Entities)
                    {
                        Ball ball = Current as Ball;

                        if (ball != null)
                        {
                            float Dist = Vector2.Distance(m_Pos, ball.m_Pos);

                            if (Dist < m_VisionRadius && ball.m_BallState == BallState.Stopped)
                            {
                                if ((!m_PrevFire) && spacePressed)
                                {
                                    m_Ball = ball;
                                    ball.m_yDirection = m_Side == PlayerSide.Down ? -1.0f : 1.0f;

                                    break;
                                }
                            }
                        }
                    }
                }
                else if (m_Ball != null) // get the ball
                {
                    m_Ball.m_Pos = m_Pos;
                }

                m_PrevFire = spacePressed;

                base.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (m_PlayerState == PlayerState.Stunned)
            {
                m_PlayerAnimatedTexture.m_Color = Color.Red;
            }
            else
            {
                m_PlayerAnimatedTexture.m_Color = Color.White;
            }

            m_PlayerAnimatedTexture.m_Origin = m_Size * 0.5f;
            //m_PlayerAnimatedTexture.DrawFrame(spriteBatch, m_Game.m_Camera.TransformPos(m_Pos));
            m_PlayerAnimatedTexture.DrawFrame(spriteBatch, m_Game.m_Camera.TransformPos(m_Pos), m_Game.m_Camera.TransformScale(m_Pos, new Vector2(m_Scale, m_Scale)));
        }
    }
}
